﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Program.cs" company="SmartPlan">
//   SmartPlan 2011
// </copyright>
// <summary>
//   Defines the Program type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
using System.Data;
using System.Data.SqlClient;

namespace DataSetCreator
{
    using System;
    using System.CodeDom;
    using System.IO;
    using DbDiff.Common.Model;
    using Microsoft.CSharp;

    /// <summary>
    /// The program.
    /// </summary>
    internal class Program
    {
        /// <summary>
        /// The main enty
        /// </summary>
        /// <param name="args">
        /// The argsuments
        /// </param>
        private static void Main(string[] args)
        {
            var server = new RegisteredServer("(local)");

            var databaseInfo = new DatabaseInfo();
            databaseInfo.Connect(server, "Adventureworks");

            /*string c =
                "SELECT SchemaName=Schema_Name(cast(ObjectPropertyex(i.object_id,'schemaid') as nvarchar(128))),TableName=object_name(i.object_id),Name=i.name,IndexFillFactor=cast(i.fill_factor as int),IsClustered=cast(CASE WHEN i.type = 1 THEN 1 ELSE 0 END as bit),IsFullTextKey=cast(indexproperty(i.object_id,i.name,'IsFullTextKey') as bit) ,IsPadIndex=cast(is_padded as bit),IsPageLockAllowed=cast(allow_page_locks ^ 1 as bit),IsRowLockAllowed=cast(allow_row_locks ^ 1 as bit),IndexFileGroup=FILEGROUP_NAME( i.data_space_id ),IdxCnstIsDisabled=cast(is_disabled as bit),IndId=cast(i.index_id as int) FROM sys.indexes As i LEFT JOIN sys.objects As o ON i.name=o.name WHERE i.name IS NOT NULL  AND (i.index_id > 0 and i.is_hypothetical = 0) AND (cast(ObjectPropertyEx(i.object_id,'IsUserTable') as bit)=1 OR cast(ObjectPropertyEx(i.object_id,'isView') as bit)=1 ) AND Cast(ObjectPropertyEx(i.object_id,'IsMSShipped') as bit)=0";

            var cb = new SqlConnectionStringBuilder
                         {
                             DataSource = "(local)",
                             InitialCatalog = "AdventureWorks2008R2",
                             IntegratedSecurity = true
                         };
            var sqlCommand = new SqlCommand(c) { Connection = new SqlConnection(cb.ConnectionString) };

            sqlCommand.Connection.Open();

            var dataSet = new DataSet();
            var dataAdapter = new SqlDataAdapter(sqlCommand);
            dataAdapter.Fill(dataSet);
            string x = dataSet.GetXmlSchema();*/

            // defaults will be good

            /*databaseInfo.SqlObjectOptions.CompareOptionsList.SetValueByName(OptionNames.CompareDatabase, "false");
            databaseInfo.SqlObjectOptions.CompareOptionsList.SetValueByName(OptionNames.CompareProc, "false");
            databaseInfo.SqlObjectOptions.CompareOptionsList.SetValueByName(OptionNames.CompareFunction, "false");
            databaseInfo.SqlObjectOptions.CompareOptionsList.SetValueByName(OptionNames.CompareTrigger, "false");
            databaseInfo.SqlObjectOptions.CompareOptionsList.SetValueByName(OptionNames.CompareView, "false");
            databaseInfo.SqlObjectOptions.CompareOptionsList.SetValueByName(OptionNames.CompareServer, "false");
            databaseInfo.SqlObjectOptions.CompareOptionsList.SetValueByName(OptionNames.CompareUdt, "false");
            databaseInfo.SqlObjectOptions.CompareOptionsList.SetValueByName(OptionNames.CompareSchema, "false");
            */

            databaseInfo.FillDatabaseInfo();
            string xml = databaseInfo.GetXmlSchema();
            xml = xml.Replace(@"<xs:element name=""IndId"" type=""xs:short"" minOccurs=""0"" />",
                              @"<xs:element name=""IndId"" type=""xs:int"" minOccurs=""0"" />");
            string output;

            using (var cscp = new CSharpCodeProvider())
            {
                var ccu = new CodeCompileUnit();
                var cn = new CodeNamespace("DbDiff.Common.DataAccess");
                output = System.Data.Design.TypedDataSetGenerator.Generate(xml, ccu, cn, cscp);
                using (var codeWriter = new StringWriter())
                {                    
                    cscp.GenerateCodeFromNamespace(cn, codeWriter, null);
                    cscp.GenerateCodeFromCompileUnit(ccu, codeWriter, null);
                    output = codeWriter.ToString();
                }
            }

            using (var writer = new StreamWriter(@"DbObjectsDs.cs", false))
            {
                writer.Write(output);
                writer.Flush();
                writer.Close();
            }

            Console.ReadLine();
        }
    }
}